package com.cloud.oss.web.controller;


import com.cloud.oss.core.utils.CommonUtil;
import com.cloud.oss.core.utils.SignUtil;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/turing/api/wechatTool/msg")
public class MsgManageController extends BaseController {
    private Logger log = CommonUtil.getLogger();


    @ResponseBody
    @RequestMapping("/autoReply")
    public void msgReturnAutomatic(HttpServletRequest request,HttpServletResponse response)throws Exception{
        String echostr = request.getParameter("echostr");
        if(echostr != null ) {
            String signature = request.getParameter("signature");
            //时间戳
            String timestamp = request.getParameter("timestamp");
            //随机数
            String nonce = request.getParameter("nonce");
            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
                log.info("[signature: "+signature + "]<-->[timestamp: "+ timestamp+"]<-->[nonce: "+nonce+"]<-->[echostr: "+echostr+"]");
                response.getOutputStream().println(echostr);
            }
            return ;
        }
        String xmlRequest = CommonUtil.getStringFromHttpServletRequest(request);
        log.info("收到通知："+xmlRequest);
        if(xmlRequest == null) {
            log.warn("消息自动回复解析出错！/r/n");
            return  ;
        }

        Map<String, String> mapRequest = null;
        try {
            mapRequest = CommonUtil.xmlToMap(xmlRequest);
        }catch(Exception e){
            log.warn(e.getMessage());
            return ;
        }
        String wechatName = mapRequest.get("ToUserName");//开发者微信号
        String userName = mapRequest.get("FromUserName");//发送方帐号（一个OpenID）
        String event = mapRequest.get("Event");//事件类型，已关注用户为SCAN，未关注用户关注后为subscribe
        String eventKey = mapRequest.get("EventKey");//事件KEY值，qrscene_为前缀，后面为二维码的参数值;或创建二维码时的二维码scene_id
        String ticket = mapRequest.get("Ticket");//二维码的ticket，可用来换取二维码图片

//        String msgReply = Constant.msgReplyMap.get(ticket);
//        Map<String,String> replyMap = new HashMap<String, String>();
//        replyMap.put("ToUserName",userName);
//        replyMap.put("FromUserName",wechatName);
//        replyMap.put("CreateTime",String.valueOf(System.currentTimeMillis()));
//        replyMap.put("MsgType","news");
//        replyMap.put("Content",msgReply);
//        replyMap.put("ArticleCount","1");

        String xmlReply = null;
        xmlReply =  "<xml>\n" +
                "<Articles>\n" +
                "<item>\n" +
                "<Title>标题</Title>\n" +
                "<Description>描述</Description>\n" +
                "<PicUrl>https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1518063229418&amp;di=63a739a7f2669c902d1c9ba345c68509&amp;imgtype=0&amp;src=http%3A%2F%2Fwww.toycraft.cn%2Fwp-content%2Fuploads%2F2013%2F07%2FUltraman-ACE-2.jpg</PicUrl>\n"+
                "<Url>http://mp.weixin.qq.com/s?__biz=MzIzNTU0ODMxNw==&amp;mid=100000260&amp;idx=1&amp;sn=34734abbaf96bf6ec0b8db209974364c&amp;chksm=68e439f65f93b0e0adc17334e0e7a0ad18d90d2c0df89534f9ebd862a0435ead4bbf53ac4f69#rd</Url>\n" +
                "</item>\n" +
                "</Articles>\n" +
                "<CreateTime>12345678</CreateTime>\n" +
                "<ArticleCount>1</ArticleCount>\n" +
                "<ToUserName>"+wechatName+"</ToUserName>\n" +
                "<FromUserName>"+userName+"</FromUserName>\n" +
                "<MsgType>news</MsgType>\n" +
                "</xml>";
//        try {
//            xmlReply = CommonUtil.mapToXml(replyMap);
//        }catch(Exception e){
//            e.printStackTrace();
//            return null;
//        }

        response.getOutputStream().println(echostr);;
    }

}
